package com.springvision.yuanfeng.common.web;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.editor.language.json.converter.util.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeeplus.common.utils.JsonTransformer;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.modules.sys.entity.Office;
import com.jeeplus.modules.sys.entity.User;
import com.jeeplus.modules.sys.mapper.OfficeMapper;
import com.jeeplus.modules.sys.mapper.UserMapper;
import com.jeeplus.modules.sys.service.SystemService;
import com.springvision.yuanfeng.device.entity.Device;
import com.springvision.yuanfeng.device.entity.DeviceMessage;
import com.springvision.yuanfeng.device.facade.DeviceFacade;
import com.springvision.yuanfeng.device.service.DeviceService;
import com.springvision.yuanfeng.device.service.DeviceStatService;
import com.springvision.yuanfeng.locale.entity.DevStation;
import com.springvision.yuanfeng.locale.service.DevStationService;
import com.springvision.yuanfeng.weather.entity.Weather;
import com.springvision.yuanfeng.weather.service.WeatherService;

/**
 * Created by Brush on 2018/7/20.
 */

@Controller
@RequestMapping(value = "${adminPath}/app/screen/show")
public class ScreenAppShowController extends BaseController {

	@Autowired
	private DevStationService devStationService;

	@Autowired
	private DeviceStatService deviceStatService;

	@Autowired
	private WeatherService weatherService;

	@Autowired
	private SystemService systemService;

	@Autowired
	private UserMapper userMapper;

	@Autowired
	private OfficeMapper officeMapper;

	@Autowired
	private DeviceService deviceService;

	@Autowired
	private DeviceFacade deviceFacade;

	/**
	 * 跳转到大屏页面
	 */
	@RequestMapping(value = "info")
	public String list(DevStation devStation, Model model) {
		model.addAttribute("devStation", devStation);

		return "yuanfeng/display/index";
	}

	/**
	 * 场站相关信息
	 */
	@ResponseBody
	@RequestMapping(value = "localInfo")
	public DevStation localInfo(@RequestParam(required = false) String id) {
		DevStation entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = devStationService.get(id);
		}
		if (entity == null) {
			entity = new DevStation();
		}
		return entity;
	}

	@ResponseBody
	@RequestMapping(value = "picture")
	public String getPicture(@RequestParam(required = true) String staId) {
		return JsonTransformer.object2Json(deviceFacade.getDevice(staId));
	}

	/**
	 * 气象信息
	 */
	@ResponseBody
	@RequestMapping(value = "weatherInfo")
	public Weather weatherInfo(@RequestParam(required = false) String id) {

		Weather weather = null;
		if (StringUtils.isNotBlank(id)) {
			DevStation entity = devStationService.get(id);
			Weather wTemp = new Weather();
			wTemp.setDevStation(entity);
			List<Weather> weathers = weatherService.findList(wTemp);
			if (weathers.size() > 0) {
				weather = weathers.get(0);
			}
		}
		return weather;
	}

	/**
	 * 设备水井信息
	 */
	@ResponseBody
	@RequestMapping(value = "deviceInfo")
	public List<DeviceMessage> deviceInfo(@RequestParam(required = false) String id) {
		return deviceStatService.getAllDeviceValueNow(id, null);
	}

	/**
	 * 设备水井统计信息
	 */
	@ResponseBody
	@RequestMapping(value = "deviceByHour")
	public Map deviceByHour(@RequestParam(required = false) String id, @RequestParam(required = false) String day) {
		Map resultMap = new HashMap();
		List<Device> deviceList = deviceService.findDeviceCode(id);
		if (CollectionUtils.isNotEmpty(deviceList)) {
			for (Device dev : deviceList) {
				resultMap.put(dev.getId() + "|||" + dev.getName(),
						deviceStatService.getDeviceValueNowByHours(dev.getId(), day));
			}
		}
		return resultMap;
	}

	/**
	 * 用户登录信息
	 */
	@ResponseBody
	@RequestMapping(value = "login")
	public User login(User user, Model model) {
		User resultUser = null;
		User usrTmp = systemService.getUserByLoginName(user.getLoginName());
		if (usrTmp != null) {
			if (SystemService.validatePassword(user.getPassword(), usrTmp.getPassword())) {
				resultUser = usrTmp;
				return resultUser;
			}
		}
		return null;
	}

	/**
	 * 获取用户所有的部门信息
	 */
	@ResponseBody
	@RequestMapping(value = "listUserOffice")
	public List<Office> listUserOffice(@RequestParam(required = false) String id) {
		List<Office> officeList = new ArrayList<>();
		User user = userMapper.get(id);
		if (user != null) {
			Map map = new HashMap();
			map.put("id", user.getOffice().getId());
			map.put("parentIds", "," + user.getOffice().getId() + ",");
			officeList = officeMapper.findAllChild(map);
		}
		return officeList;
	}

	/*
	 * 根据部门获取所有的场站信息
	 */

	@ResponseBody
	@RequestMapping(value = "listLocal")
	public Map<String, Object> listLocal(DevStation devStation, HttpServletRequest request,
			HttpServletResponse response, Model model) {
		if (StringUtils.isNotEmpty(devStation.getOfficeId())) {
			String[] arr = devStation.getOfficeId().split(",");
			List<String> offLst = Arrays.asList(arr);
			devStation.setOfficeLst(offLst);
		}
		Page<DevStation> page = devStationService.findPage(new Page<DevStation>(request, response), devStation);
		return getBootstrapData(page);
	}
}
